$(function(){
	var nowDate = new Date();
	if(!$("#timeStart").datebox("getValue")){
		$("#timeStart").datebox("setValue",nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+nowDate.getDate());
	}
	if(!$("#timeEnd").datebox("getValue")){
		$("#timeEnd").datebox("setValue",nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+nowDate.getDate());
	}
	$("input[type='radio']").click(function(){
		radioFunction($(this).val());
	});
	$("#wholeMonth").click(function(){
		if($(this).prop("checked")){
			var timeStart = new Date($("#timeStart").datebox("getValue"));
			var timeEnd =  new Date($("#timeEnd").datebox("getValue"));
			$("#timeStart").datebox("setValue",timeStart.getFullYear()+'-'+(timeStart.getMonth()+1)+'-01');
            $("#timeEnd").datebox("setValue",timeEnd.getFullYear()+'-'+(timeEnd.getMonth()+1)+'-'+new Date(timeEnd.getFullYear(),(timeEnd.getMonth()+1),0).getDate());
		}
	});
});

function checkRadio(val){
	$.each($("input[type='radio']"),function(i,v){
		if(val==i){
			$(v).attr("checked","checked");
			$(v).click();
		}else{
			$(v).removeAttr("checked");
		}
	})
}

function radioFunction(val){
	var nowDate = new Date();
	switch(val){
	case "0":  //小于，开始时间禁用
		$("#timeStart").datebox("setValue","1900-01-01");
		$("#timeStart").datebox("disable");
		$("#timeStart").prev().addClass("disabled");
		
		$("#timeEnd").datebox("enable");
		$("#timeEnd").prev().removeClass("disabled");
		console.info($("#timeEnd").datebox("getValue"));
		if($("#timeEnd").datebox("getValue")=="3000-01-01" || $("#timeEnd").datebox("getValue").length==0){
			$("#timeEnd").datebox("setValue",nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+nowDate.getDate());
		}
		break;
	case "1":  //介于
		$("#timeStart").datebox("enable");
		$("#timeEnd").datebox("enable");
		$("#timeStart").prev().removeClass("disabled");
		$("#timeEnd").prev().removeClass("disabled");
		
		if($("#timeStart").datebox("getValue")=="1900-01-01" || $("#timeStart").datebox("getValue").length==0){
			$("#timeStart").datebox("setValue",nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+nowDate.getDate());
		}
		if($("#timeEnd").datebox("getValue")=="3000-01-01" || $("#timeEnd").datebox("getValue").length==0){
			$("#timeEnd").datebox("setValue",nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+nowDate.getDate());
		}
		break;
	case "2":  //大于,截止时间禁用
		$("#timeStart").datebox("enable");
		$("#timeStart").prev().removeClass("disabled");
		if($("#timeStart").datebox("getValue")=="1900-01-01" || $("#timeStart").datebox("getValue").length==0){
			$("#timeStart").datebox("setValue",nowDate.getFullYear()+"-"+(nowDate.getMonth()+1)+nowDate.getDate());
		}
		
		$("#timeEnd").datebox("setValue","3000-01-01");
		$("#timeEnd").datebox("disable");
		$("#timeEnd").prev().addClass("disabled");
		break;
	case "3":
		$("#timeStart").datebox("setValue","");
		$("#timeEnd").datebox("setValue","");
	}

}

function submit(){
	var timeStart = $("#timeStart").datebox("getValue");
	var timeEnd = $("#timeEnd").datebox("getValue");
	if(timeStart>timeEnd){
		$.messager.alert('提示','终止时间大于起始时间，请重新选择','info');
		return;
	}
	var timeScope = $("#timeScope").val();
	if(timeScope && timeScope.length>0){
		var min = timeScope.split(",")[0];
		var max = timeScope.split(",")[1];
		if(min && min.length>0 && timeStart<min){
			$.messager.alert('提示','起始时间不可小于 '+min+',请重新选择','info');
			return;
		}
		if(max && max.length>0 && timeEnd>max){
			$.messager.alert('提示','终止时间不可大于'+max+'，请重新选择','info');
			return;
		}
	}
	window.parent.writeDateBack(timeStart,timeEnd);
}

function cancel(){
	window.parent.closeDialog();
}

/**
 * 改变下拉月份，自动填充时间
 */
function changeTime(val){
	//默认当天、一周、一个月、二个月,三个月,一年,全部
	var nowDate = new Date();
	var nowDate1 = new Date();
	var m = $('#shortCut').val();
	if(m==1){ //昨天
		nowDate.setDate(nowDate.getDate()-1);
		nowDate1.setDate(nowDate1.getDate()-1);
	}else if(m==2){ //今天
		
	}else if(m==3){//当月
		nowDate = new Date(nowDate.getFullYear(),nowDate.getMonth(),1);
		nowDate1 = new Date(nowDate.getFullYear(),(nowDate.getMonth()+1),0);
	}else if(m==4){//近一周
		nowDate.setDate(nowDate.getDate()-7); 
	}else if(m==5){//近一个月
		nowDate.setMonth(nowDate.getMonth()-1); 
	}else if(m==6){//近三个月
		nowDate.setMonth(nowDate.getMonth()-3); 
	}else if(m==7){//近半年
		nowDate.setMonth(nowDate.getMonth()-6); 
	}else if(m==8){//近一年
		nowDate.setFullYear(nowDate.getFullYear()-1); 
	}else if(m==9){ //前一周
		/*nowDate1.setDate(nowDate1.getDate()-nowDate1.getDay()); 
		nowDate.setDate(nowDate1.getDate()-6);*/
		nowDate1 = addDate(nowDate1,0-nowDate1.getDay());
		nowDate = addDate(nowDate1,-6);
	}else if(m==10){ //本周
		/*nowDate.setDate(nowDate.getDate()-nowDate.getDay()+1); 
		nowDate1.setDate(nowDate.getDate()+6);*/
		nowDate = addDate(nowDate,1-nowDate.getDay());
		nowDate1 = addDate(nowDate,6);
	}else if(m==11){ //下一周
		/*nowDate.setDate(nowDate.getDate()+(7-nowDate.getDay())+1);
		nowDate1.setDate(nowDate.getDate()+6);*/
		nowDate = addDate(nowDate,(7-nowDate.getDay())+1);
		nowDate1 = addDate(nowDate,6);
	}else if(m==12){ //前一个月
		nowDate = new Date(nowDate.getFullYear(),nowDate.getMonth()-1,1);
		nowDate1 = new Date(nowDate1.getFullYear(),(nowDate1.getMonth()),0);
	}else if(m==13){  //本月
		nowDate = new Date(nowDate.getFullYear(),nowDate.getMonth(),1);
		nowDate1 = new Date(nowDate1.getFullYear(),(nowDate1.getMonth()+1),0);
	}else if(m==14){  //后一个月
		nowDate = new Date(nowDate.getFullYear(),nowDate.getMonth()+1,1);
		nowDate1 = new Date(nowDate1.getFullYear(),(nowDate1.getMonth()+2),0);
	}
	var strDate = nowDate.getFullYear()+"-";
	    strDate += nowDate.getMonth()+1+"-";
	    strDate += nowDate.getDate();
	   
	var strDate1 = nowDate1.getFullYear()+"-";
	    strDate1 += nowDate1.getMonth()+1+"-";
	    strDate1 += nowDate1.getDate();
	    $('#timeStart').datebox("setValue",strDate);
	    $('#timeEnd').datebox("setValue",strDate1);
}

function addDate(date,n){  
	return new Date(date-0+n*86400000);  
} 